package sw6.girafAppKit;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

/**
 * Is used specifically by sw6.girafAppKit.
 * 
 * @author Michael & Nikolaj - SW6C
 * 
 */
public abstract class GirafActivity extends Activity {
	protected String parentIntent =Intent.ACTION_MAIN; //Shows a list of apps, who has an MAIN-intent. (the parental interface in future sprint)
	int volCount = 0;
	private final String logTag= "GIRAF";
	
	/**
	 * The function is used for accessing the parental interface, which at the given time
	 * is alternating pressing the Volume Up and Volume Down button, 6 times in total
	 * (Up, Down, Up, Down, Up, Down).
	 * If, during the sequence, a wrong sequence of buttons is pressed, the sequence 
	 * must be pressed from the start again.
	 * NOTE: The End-call and Home buttons can not be handled.
	 * @param event The KeyEvent to be handled, for instance: KeyEvent.ACTION_DOWN.
	 * @param keyCode An integer, used in the switch-case of this function.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if (event.getAction() == KeyEvent.ACTION_DOWN){
			switch(keyCode){
				case KeyEvent.KEYCODE_ENDCALL:
				case KeyEvent.KEYCODE_HOME:
					return false;
				case KeyEvent.KEYCODE_VOLUME_DOWN:
					if (volCount % 2 == 1 && volCount < 6) {
						volCount++;
					} else {
						volCount = 0;
					}
					break;
				case KeyEvent.KEYCODE_VOLUME_UP:
					if (volCount % 2 == 0 && volCount < 6) {
						volCount++;
					} else {
						volCount = 0;
					}
					break;
				default:
					break;
			}
			if (volCount == 6) {
				try {
					startActivity(new Intent(parentIntent));
				} catch(ActivityNotFoundException e){
					Log.e(logTag, parentIntent + " is not allowed");
				}
			}
			return true;
		}
		return false;
	}
}
